package com.sxzbxc.weixin.util;

import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import com.sxzbxc.weixin.entity.SysUser;

public class SessionIntercept implements  HandlerInterceptor   {

	private List<String> excludedUrls;
	
	public void setExcludedUrls(List<String> excludedUrls) {
		  this.excludedUrls = excludedUrls;
	}
	
	@Override
	public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3)
			throws Exception {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3)
			throws Exception {
		// TODO Auto-generated method stub
		
	}

	@Override
	public boolean preHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2) throws Exception {
		String requestPath = ResourceUtil.getRequestPath(arg0);// 用户访问的资源地址
		for (String url : excludedUrls) {
			if (requestPath.equals(url)) {
			    return true;
			}
		}
		//从session获取登录信息  
		if((SysUser)arg0.getSession().getAttribute("user")==null){  
			arg1.sendRedirect(arg0.getContextPath()+"/index.html");
            return false;  
        }
		return true;  
	}
}
